﻿Imports System.Windows.Forms
Imports Microsoft.Xna.Framework.Content.Pipeline.Graphics

Public Class FormProgress

    Public Property Input() As FontDescription
    Public Property WorkerThread() As Threading.Thread
    Public Property Dict() As New Dictionary(Of Char, Integer)

    Public Property StartIndex() As Integer = Short.MinValue
    Public Property EndIndex() As Integer = Short.MaxValue
    Public Property Offset() As Integer = Me.EndIndex - Me.StartIndex

    Public Property StartTime() As DateTime

    Public Sub New(input As FontDescription)
        InitializeComponent()
        Me.Input = input
        Me.WorkerThread = New Threading.Thread(
            Sub()
                Try
                    Dim currentIndex As Integer = Me.StartIndex
                    Do While currentIndex <= Me.EndIndex
                        If FontProcessor.Cancel = False Then
                            Dim currentChar = ChrW(currentIndex)
                            If input.Characters.Contains(currentChar) = False Then
                                input.Characters.Add(currentChar)
                            End If
                            Threading.Thread.Sleep(10)
                            Me.UpdateProgress(currentIndex)
                        Else
                            MessageBox.Show(String.Format("Canceled at {0}, {1}!", currentIndex.ToString(), ChrW(currentIndex)))
                            Exit Do
                        End If
                        currentIndex += 1
                    Loop
                Catch ex As Exception
                    MessageBox.Show(ex.Message & ex.StackTrace.ToString())
                End Try
            End Sub)
    End Sub

    Public Sub UpdateProgress(ByVal index As Integer)
        Me.BeginInvoke(
            Sub()
                Dim Count = index - Me.StartIndex
                Me.LB_Count.Text = Count.ToString()
                Dim RemainingCount = Me.Offset - Count
                Me.LB_CurrentChar.Text = ChrW(index)
                Dim percentage = (Count / Me.Offset) * 100
                Me.PB_Progress.Value = CInt(percentage)
                Me.LB_Percentage.Text = String.Format("{0:N2}%", percentage)
                Dim elapsedTime = Now - Me.StartTime
                Me.LB_ElapsedTime.Text = elapsedTime.ToString()
                Dim speed As Double = 0
                If elapsedTime.TotalSeconds = 0 Then
                    speed = 0
                Else
                    speed = Count / elapsedTime.TotalSeconds
                End If
                Me.LB_Speed.Text = CLng(speed).ToString()
                If speed = 0 Then speed = 1
                Dim ticks = CLng(RemainingCount / speed * Math.Pow(10, 7))
                Dim timeRemaining As New TimeSpan(ticks)
                Me.LB_TimeRemaining.Text = timeRemaining.ToString()
                If index = Me.EndIndex Then
                    Threading.Thread.Sleep(500)
                    Me.Close()
                End If
            End Sub)
    End Sub

    Private Sub FormProgress_FormClosing(sender As Object, e As Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        FontProcessor.Cancel = True
    End Sub

    Private Sub FormProgress_Load(sender As Object, e As EventArgs) Handles Me.Load
        Me.StartTime = Now
        Me.WorkerThread.Start()
    End Sub

End Class